<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="heaerTopContent">
        <div class="grid-content bg-purple">
          <!-- <el-input v-model.trim="listQuery.createUserName" placeholder="" style="width: 350px;" class="filter-item" @keyup.enter.native="handleFilter" /> -->
          <el-input v-model.trim="listQuery.title" placeholder="请输入搜索标题" clearable style="width: 300px;" class="filter-item" @keyup.enter.native="handleFilter" />
          <el-button class="filter-item" type="primary" style="margin-left: 10px;" icon="el-icon-search" @click="handleFilter">
            搜索
          </el-button>
        </div>
        <div class="grid-content bg-purple">
          <el-button style="margin-left: 10px;" type="danger" icon="el-icon-plus" @click="createdAdvertising">创建广告</el-button>
        </div>
      </div>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" :default-sort="{prop:'title',order:'descending'}">
      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          <span>{{ (listQuery.pageNo-1)*listQuery.pageSize+scope.$index +1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="标题" prop="title" sortable>
        <template slot-scope="{row}">
          <el-tooltip class="item" effect="light" :content="row.title" placement="top">
            <span class="ellipsis">{{ row.title }}</span>
          </el-tooltip>

        </template>
      </el-table-column>
      <el-table-column align="center" label="链接地址">
        <template slot-scope="{row}">
          <!-- <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span> -->
          <el-tooltip class="item" effect="light" :content="row.link" placement="top">
            <span class="ellipsis">{{ row.link }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" label="图片地址">
        <template slot-scope="{row}">
          <el-tooltip class="item" effect="light" :content="row.picLink" placement="top">
            <span class="ellipsis">{{ row.picLink }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" label="分组" width="120">
        <template slot-scope="{row}">
          <span><el-tag type="parimary" effect="plain">{{ row.advertisingGroup }}</el-tag></span>
        </template>
      </el-table-column>

      <!-- <el-table-column width="100px" label="Importance">
        <template slot-scope="scope">
          <svg-icon v-for="n in +scope.row.importance" :key="n" icon-class="star" class="meta-item__icon" />
        </template>
      </el-table-column> -->

      <el-table-column class-name="status-col" align="center" label="状态" width="120">
        <template slot-scope="{row}">
          <el-tag :type="row.status==0?'success':'danger'">
            {{ row.status | statusFilter }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="handleDetail(scope.$index, scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="Actions" width="120">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit">
            Edit
          </el-button>
          <router-link :to="'/example/edit/'+scope.row.id">
            <el-button type="primary" size="small" icon="el-icon-edit">
              Edit
            </el-button>
          </router-link>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="pageGation">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getadvertAllList" />
    </div>
    <!-- 新增弹窗 -->
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="centerDialogVisible"
      width="40%"
      center
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="广告标题" prop="title">
          <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="广告地址" prop="link">
          <el-input v-model.trim="ruleForm.link" />
        </el-form-item>
        <el-form-item label="图片地址" prop="picLink">
          <el-input v-model.trim="ruleForm.picLink" />
        </el-form-item>
        <el-form-item label="分组" prop="group">
          <el-input v-model="ruleForm.advertisingGroup" />
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="centerDialogVisible=false">取 消</el-button>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogStatus==='create'?createData():editData()">
          确定
        </el-button>
        <el-button @click="centerDialogVisible = false">
          取消
        </el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog :visible.sync="dialogPvVisible" title="查看详情" width="40%">
      <div v-if="detailInfo" class="itemInfoBox">
        <el-divider />
        <div class="textItem">ID：{{ detailInfo.advertisingId }}</div>
        <div v-if="detailInfo.createTime" class="textItem">创建时间：{{ detailInfo.createTime }}</div>
        <div v-if="detailInfo.deleteTime" class="textItem">删除时间：{{ detailInfo.deleteTime }}</div>
        <el-divider />

        <div class="textItem">标题：{{ detailInfo.title }}</div>
        <div class="textItem">广告链接：{{ detailInfo.link }}</div>
        <div class="textItem">图片地址：{{ detailInfo.picLink }}</div>
        <el-divider />
        <div class="textItem">分组：<el-tag type="danger">{{ detailInfo.advertisingGroup?detailInfo.advertisingGroup:'暂无' }}</el-tag></div>
        <div class="textItem">状态：<el-tag :type="detailInfo.status==0?'success':'warning'">{{ detailInfo.status===0?'正常':'停用' }}</el-tag></div>
        <el-divider />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import { fetchList } from '@/api/article'
import { createAdvert, getAdvertlist, deleteAdvert, AdvertDetail, editAdvert } from '@/api/addAdvert'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: 'ArticleList',
  components: { Pagination },
  // 状态过滤器
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: '正常',
        1: '停用'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      centerDialogVisible: false,
      dialogPvVisible: false,
      listLoading: false,
      detailInfo: null,
      listQuery: {
        title: '',
        pageNo: 1,
        pageSize: 20
      },
      textMap: {
        edit: '修改广告',
        create: '新建广告'
      },
      dialogStatus: '',
      ruleForm: {
        advertisingId: undefined,
        title: '',
        link: '',
        picLink: '',
        advertisingGroup: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入广告标题', trigger: 'blur' }
        ],
        link: [
          { required: true, message: '请输入广告链接地址', trigger: 'blur' }
        ],
        picLink: [
          { required: true, message: '请输入图片地址', trigger: 'blur' }
        ],
        advertisingGroup: [
          { required: true, message: '请输入分组', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getadvertAllList()
  },
  methods: {
    getadvertAllList() {
      this.listLoading = true
      getAdvertlist(this.listQuery).then(res => {
        if (res.code === '00000') {
          this.listLoading = false
          this.list = res.data.rows
          this.total = res.data.totalRows
        } else {
          console.log('请求错误')
        }
      })
    },
    handleFilter() {
      this.loading = true
      setTimeout(() => {
        this.loading = false
        this.getadvertAllList()
      }, 200)
    },
    resetDataForm() {
      this.ruleForm = {
        advertisingId: undefined,
        title: '',
        link: '',
        picLink: '',
        group: ''
      }
    },
    // 新增广告
    createdAdvertising() {
      this.resetDataForm()
      this.dialogStatus = 'create'
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    // 创建
    createData() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createAdvert(this.ruleForm).then(res => {
            if (res.code === '00000') {
              this.$message.success('添加成功')
              this.centerDialogVisible = false
              setTimeout(() => {
                this.getadvertAllList()
              }, 500)
            }
          })
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 编辑
    handleEdit(index, row) {
      this.ruleForm = Object.assign({}, row) // 克隆数据
      this.dialogStatus = 'edit'
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    editData() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          editAdvert(this.ruleForm).then((res) => {
            if (res.code === '00000') {
              this.centerDialogVisible = false
              this.$message.success('修改成功')
              setTimeout(() => {
                this.getadvertAllList()
              }, 200)
            }
          })
        }
      })
    },
    handleDelete(index, row) {
      const advertisingId = row.advertisingId
      console.log(advertisingId)
      deleteAdvert({ advertisingId }).then(res => {
        if (res.code === '00000') {
          this.$message.success('删除成功')
          setTimeout(() => {
            this.getadvertAllList()
          }, 200)
        }
      })
    },
    handleDetail(index, item) {
      this.dialogPvVisible = true
      const advertisingId = item.advertisingId
      AdvertDetail({ advertisingId }).then(res => {
        if (res.code === '00000') {
          this.detailInfo = res.data
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.ellipsis{
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
}
  .itemInfoBox{
    .textItem{
        padding:8px 25px;

      }
  }
.heaerTopContent{
  height: 100%;
  clear: both;
  .grid-content{
    float: left;
  }
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
.pageGation{
  display: flex;
  justify-content: flex-end;
  margin: 15px 40px 0;
  font-size: 23px;
}
</style>
